/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.*;

/**
 *
 * @author plasalmonie
 */
public class control extends HttpServlet {

    private Pays p;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = "erreur.jsp";
        if (request.getParameter("choix") == null) {
            page = "accueil.jsp";
        } else if (request.getParameter("choix").equals("listedep")) {
            if (request.getParameter("depChoisis") == null) {
                Collection<Dep> lesDep = p.getLesDep();
                request.setAttribute("listeDep", lesDep);
                page = "liste_departement.jsp";
            } else {
                String numDep;
                numDep = request.getParameter("depChoisis");
                Collection<Med> lesMed = p.getLeDep(numDep).getLesMeds();
                request.setAttribute("listeMed", lesMed);
                request.setAttribute("numDepChoisis", numDep);
                page = "listeMedParDep.jsp";
            }
        } else if (request.getParameter("choix").equals("listenom")) {
            if (request.getParameter("nomSaisis") == null) {
                page = "liste_nom.jsp";
            } else {
                String saisie = request.getParameter("nomSaisis").substring(0, 1).toUpperCase();
                Collection<Dep> lesDep = p.getLesDep();
                Collection<Med> lesMed = new TreeSet<Med>();
                for (Dep unDep : lesDep) {
                    lesMed.addAll(unDep.getLesMedsParNom(saisie));
                }
                request.setAttribute("listeMed", lesMed);
                page = "listeMedParSaisis.jsp";
            }
        } else if (request.getParameter("choix").equals("listespe")) {
            if (request.getParameter("SpeChoisis") == null) {               
                Collection<Spe> lesSpe = p.getLesSpe();
                request.setAttribute("listeSpe", lesSpe);
                page = "liste_spe.jsp";
            } else {
                String numSpe;
                numSpe = request.getParameter("SpeChoisis");
                Collection<Med> lesMed = p.getLaSpe(numSpe).getLesMeds();
                request.setAttribute("listeMed", lesMed);
                request.setAttribute("speChoisis", numSpe);
                page = "listeMedParSpe.jsp";
            }
            
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    public void destroy() {
        Connect.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
